package com.mroapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.mroapp.R;
import com.mroapp.base.BaseActivity;
import com.mroapp.utils.APP;
import com.mroapp.utils.APPCode;
import com.mroapp.utils.Sys;
import com.mroapp.utils.Util;

/**
 * Created by apple on 16/6/12.
 */
public class WelcomeActivity extends BaseActivity {
    private Class<?>cls;
    private int verCode=0;
    private Context context;
    private static final int GOTO_MAIN_ACTIVITY = 001;
    private static final int TIMEOUT = 1500; // 延迟跳转时间
    private String tag = "WelcomeActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        this.context=WelcomeActivity.this;
        VerInfo();
        CheckSDCard();
        checkCachePath();
        checkGuide();
    }


    /**
     * 设置版本更新
     */
    public void VerInfo() {
        String vername = "";
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            vername = info.versionName;
            verCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
        }
        ((TextView) findViewById(R.id.version_code)).setText(vername);
    }

    /**
     * 检查sd卡是否挂载
     */
    public void CheckSDCard(){
        if(!Sys.isSDcardExist()){
            APP.Tip("建议装在外置存储卡");
        }
    }

    /**
     * 检查缓存路径
     */
    private void checkCachePath() {
        Util util = Util.getInstance(context);
        // 检查缓存路径
        util.init_path();

        // 删除分享时的临时图片
        Sys.deleteAllFilesOfDir(Util.IMG_SHARE_PATH, false);
    }
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GOTO_MAIN_ACTIVITY:
                    Intent intent = new Intent();
                    intent.setClass(context, cls);
                    startActivity(intent);
                    animLeftToRight();
                    finish();
                    System.gc();
                    break;
            }
        }
    };

    /**
     * 检测网络是否正常
     */
    private void checkNetStatus() {
        if (!Sys.CheckNetworkState(context, false))
            Sys.confirmSetNetwork(context);
        else
            // 开始跳转
            mHandler.sendMessageDelayed(mHandler.obtainMessage(GOTO_MAIN_ACTIVITY), TIMEOUT);
    }

    /**
     * 是否展示引导页
     */
    private void checkGuide() {
        cls = HomeActivity.class;
        SharedPreferences used = getSharedPreferences(APPCode.USED, 0);
        try {
            if (verCode > used.getInt("code", 0)) {
                cls = GuideActivity.class;
                // 强制必须看完引导，没看完，下次启动继续看  然后将app的versionCode存入sharedprefrence中
                 used.edit().putBoolean("used", true).putInt("code",verCode).commit();//不强制
            } else
                if((Boolean)(spUtils.get("login",false))){
                    cls = HomeActivity.class;
                }else{
                    cls=LoginActivity.class;
                }

        } catch (Exception e) {
            APP.exception(tag + " checkGuide ", e);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        checkNetStatus();
    }



}
